import time

from appium.options.android import UiAutomator2Options
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction


class CommonDriver:
    def get_driver(self):
        # 初始化移动端设备的参数
        options =UiAutomator2Options()
        options.platform_name = "Android"
        options.platform_version = '7'
        options.app_package="com.dangdang.buy2" #当当网
        # options.app_package = "com.microvirt.launcher2"  #桌面
        options.app_activity="com.dangdang.buy2.StartupActivity"#当当网
        # options.app_activity = "com.microvirt.launcher.Launcher"#桌面
        options.no_reset = False
        options.unicode_keyboard=True
        options.uuid="127.0.0.1:21513"

        # 设置好appium服务器的url
        command_executor_url = "http://127.0.0.1:4723/wd/hub"

        driver = webdriver.Remote(command_executor=command_executor_url, options=options)

        driver.implicitly_wait(10)

        print("连接成功")

        return driver


if __name__ == '__main__':
    driver = CommonDriver().get_driver()
    # driver.find_element(AppiumBy.ID,"com.dangdang.buy2:id/tv_sure").click()
    # # driver.find_elements(AppiumBy.CLASS_NAME,"android.widget.TextView")[3].click()
    # driver.find_element(AppiumBy.XPATH,"//android.widget.TextView[@text='同意']").click()   #xpash文本定位
    # driver.find_element(AppiumBy.XPATH,"//android.widget.TextView[contains(@text,'不同')]").click()  #模糊文本定位
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure" and @text="同意"]').click()


    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()

    ''' # 搜索'''
    # driver.find_element(AppiumBy.XPATH,'//android.widget.FrameLayout[@resource-id="com.dangdang.buy2:id/home_title_layout"]/android.widget.LinearLayout').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys("浪潮之巅")
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_search"]').click()
    # driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/etv_delete_key"]').click()
    # driver.quit()

    # try:
    #     screen_size=driver.get_window_size()
    #     print("---------------",screen_size)
    #     width=screen_size['width']
    #     height=screen_size['height']
    #
    #     # 定义滑动的起始和结束坐标
    #     start_x=width/2
    #     start_y=height*0.8
    #     end_x=width/2
    #     end_y=height*0.2
    #     print(start_x, start_y, end_x, end_y)
    #     driver.swipe(start_x, start_y, end_x, end_y)
    #     time.sleep(2)
    # finally:
    #     driver.quit()

    # 在指定坐标处点击
    # driver.tap([(55,415)],500)

    element = driver.find_element(AppiumBy.XPATH, '//android.widget.TextView[@text="当当"]')
    super_driver = TouchAction(driver)
    # 链式调用
    super_driver.long_press(element).move_to(x=101, y=601).release().perform()










